package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.ContractInfo;
import ywpt.model.Department;
import ywpt.model.GroupInfo;
import ywpt.model.JobInfo;
import ywpt.model.PeopleInfo;
import ywpt.service.config.DepartmentService;
import ywpt.service.config.GroupService;
import ywpt.service.contract.ContractService;
import ywpt.service.people.PeopleService;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class DepartmentAction extends BaseAction implements
		ModelDriven<Department>, Preparable {

	/**
	 * 部门Action
	 */
	private static final long serialVersionUID = -9031703786393985104L;
	private static int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	private String departCode;
	private String departName;
	private String remark;

	private Department department;
	private DepartmentService departmentService;
	private List<Department> departmentList = new ArrayList<Department>();

	private GroupService groupService;
	private ContractService contractService;
	private PeopleService peopleService;
		


	public ContractService getContractService() {
		return contractService;
	}

	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}

	public PeopleService getPeopleService() {
		return peopleService;
	}

	public void setPeopleService(PeopleService peopleService) {
		this.peopleService = peopleService;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getDepartCode() {
		return departCode;
	}

	public void setDepartCode(String departCode) {
		this.departCode = departCode;
	}

	public String getDepartName() {
		return departName;
	}

	public void setDepartName(String departName) {
		this.departName = departName;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public List<Department> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<Department> departmentList) {
		this.departmentList = departmentList;
	}

	public GroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	// ****************************默认方法*******************************

	@Override
	@SuppressWarnings("unchecked")
	public String execute() {
		return null;
	}

	@Override
	public Department getModel() {
		return this.department;
	}

	@Override
	public void prepare() throws Exception {
		if (id != null) {
			this.department = departmentService.getDepartmentById(id);
		} else {
			this.department = new Department();
		}
	}

	// **************************数据交互方法*******************************
	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(Department.class);
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = departmentService.getDepartmentCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("departCode"));
		query.setProjection(null);
		departmentList.addAll(departmentService.getDepartments(query, pageSize,
				rowStartEnd[0]));
		return SUCCESS;
	}

	public String input() {
		String departCode = this.departmentService.creatDepartCode();
		department.setDepartCode(departCode);
		return INPUT;
	}

	public String edit() {
		this.department = departmentService.getDepartmentById(id);
		return INPUT;
	}
	
	public String save() {
		try {
			if (id == null) {
				departmentService.saveDepartment(department);
				this.addActionMessage("部门信息新增成功!");				
			} else {
					departmentService.updateDepartment(department);		
					this.addActionMessage("部门信息更新成功!");		
			}
			return SUCCESS;
		} catch (UniqueConstraintException e) {
				this.addActionError("保存错误:" + e.getMessage());
			return INPUT;
		}
	}

	public String delete() throws Exception {
		if (id != null) {
			
			DetachedCriteria contractQuery = DetachedCriteria.forClass(ContractInfo.class);
			contractQuery.add(Restrictions.eq("depart", departmentService.getDepartmentById(id)));
			int totalRows1 = departmentService.getDepartmentCountByCriteria(contractQuery);

			DetachedCriteria peopleQuery = DetachedCriteria.forClass(PeopleInfo.class);
			peopleQuery.add(Restrictions.eq("depart", departmentService.getDepartmentById(id)));
			int totalRows2 = peopleService.getPeopleInfoCountByCriteria(peopleQuery);

			DetachedCriteria groupQuery = DetachedCriteria.forClass(GroupInfo.class);
			groupQuery.createAlias("depart","depart").add(Restrictions.eq("depart.id",id));
			int totalRows4 = groupService.getGroupInfoCountByCriteria(groupQuery);
			
			if (totalRows1>0 || totalRows2>0 || totalRows4>0) {
				this.addActionMessage("该客户已经发生过业务逻辑，不能删除！");
				return INPUT;
			}else{
				departmentService.removeDepartment(id);
				return SUCCESS;				
			}

		}
		this.addActionMessage("部门编号为空，请选择具体的记录！");
		return INPUT;
	}
	
	
	
	
	public String select() 
	{
		departmentList.addAll(departmentService.getDepartInfos());
		return SUCCESS;
	}


}
